// var FacilityShow = FacilityShow || {};
FacilityShow = function(stageSize,container) {
	this.scene = new THREE.Scene();
	this.camera = new THREE.PerspectiveCamera(70, stageSize.width/stageSize.height, 1, 1000);
	this.camera.position.y = 100;
	this.camera.position.z = 200;
	this.scene.add(this.camera);
	this.cube;
	this.targetRotation=0;
	this.targetRotationOnMouseDown;
	this.stageSize=stageSize;
	this.moveXOnMouseDown;
	this.moveX;
	
	var ambientLight = new THREE.AmbientLight( 0xffffff );
	this.scene.add( ambientLight );
	var directionalLight = new THREE.DirectionalLight( 0xffffff );
	directionalLight.position.set(0, 0.1, 1 ).normalize();
	this.scene.add( directionalLight );
	this.renderer = new THREE.WebGLRenderer({ clearColor: 0xF9F9EF, clearAlpha: 1 });
	this.renderer.setSize( stageSize.width, stageSize.height );
	
	var me=this;
	container.append( $(this.renderer.domElement) );
	$('canvas').bind( 'mousedown', onDocumentMouseDown );
	
function onDocumentMouseDown( event ) {
	event.preventDefault();
	$('canvas').bind( 'mousemove', onDocumentMouseMove );
	$('canvas').bind( 'mouseup', onDocumentMouseUp );
	$('canvas').bind( 'mouseout', onDocumentMouseOut );
	me.moveXOnMouseDown = event.clientX - me.stageSize.width/2;
	me.targetRotationOnMouseDown = me.targetRotation;
};

function onDocumentMouseMove( event ) {
	me.moveX = event.clientX -  me.stageSize.width/2;
	me.targetRotation = me.targetRotationOnMouseDown + ( me.moveX - me.moveXOnMouseDown ) * 0.02;
};
function onDocumentMouseUp ( event ) {
	$('canvas').unbind( 'mousemove', onDocumentMouseMove );
	$('canvas').unbind( 'mouseup', onDocumentMouseUp );
	$('canvas').unbind( 'mouseout', onDocumentMouseOut );
};

function onDocumentMouseOut( event ) {
	$('canvas').unbind( 'mousemove', onDocumentMouseMove );
	$('canvas').unbind( 'mouseup', onDocumentMouseUp );
	$('canvas').unbind( 'mouseout', onDocumentMouseOut );
};

};
FacilityShow.prototype.constructor = FacilityShow;

FacilityShow.prototype.load = function(geometry) {
	var loader = new THREE.JSONLoader();
	var me=this;
	loader.load(geometry, function(geometry) {
		geometry.computeBoundingBox();
		me.cube = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
		me.cube.position.y=10;
		me.scene.add(me.cube);
	});
};

FacilityShow.prototype.render=function () {
	if(this.cube)
	this.cube.rotation.y += ( this.targetRotation - this.cube.rotation.y ) * 0.05;
	this.renderer.render( this.scene, this.camera );
};

FacilityShow.prototype.rightRotate=function(){
	this.targetRotation+=8;
};
FacilityShow.prototype.leftRotate=function(){
	this.targetRotation-=8;
};
